package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommonResult;
import com.bilibili.entity.User;
import com.bilibili.service.VideoService;
import com.bilibili.service.impl.VideoServiceImpl;

@SuppressWarnings("serial")//抑制serialVersionUID字段警告
@WebServlet("/deleteVideos")
public class DeleteVideosServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		int user_id = ((User) request.getSession().getAttribute("loginUser")).getUserId();
		VideoService videoService = new VideoServiceImpl();
		CommonResult cr = new CommonResult();
		String[] allVideoId = request.getParameterValues("allVideoId");
		int num = 0;
		for(int i = 0;i<allVideoId.length;i++) {
			if(videoService.deleteVideosByVideoId(user_id,Integer.parseInt(allVideoId[i]))) {
				num++;
			}
		}
		if(num == allVideoId.length) {
			cr.setCode(200);
			response.getWriter().write(cr.toJSONString());
		}else {
			cr.setCode(409);
			response.getWriter().write(cr.toJSONString());
		}
	}
}
